/* eslint-disable max-len */
import { Router } from "express";
import { generateTx, getTx } from "../txHandlers";
import { generateTxSanitizers, generateTxValidators } from "../validate";

const router = Router();

const authentication = (req, res, next) => {
    // console.log("verifyed authentication");
    next();
};
router.all("*", authentication);

/** 
* @swagger 
* path:
*  /transactions/{id}:
*    get:
*      summary: 向代理服务查询交易是否已被区块链确认
*      tags: [Transactions]
*      parameters:
*        - in: path
*          name: id
*          schema:
*            type: string
*          required: true
*          description: 交易的唯一标识 
*      responses:
*        "200":
*          description: 返回查询请求成功信息
*          content:
*            application/json:
*              schema:
*                $ref: '#/components/schemas/TxGetRes'  
*              examples:
*                Confirmed:
*                  value:
*                    txid: "43092d22d62eac0e76395aa40dcecfb27e80c61b170e4f06fc05aea27aa23aff"
*                    blockHash: "NGU1N2I0YjA3NzU3MjgxZTE3MzFhMWJiYWFjYzkyNzNiNzk3ZjFmOTgyNDk3NjQ2ZDAzNWI2NjU3NzBmMGRmZg=="
*                    code: "REQUEST_SUCCESS"
*                    status: "CONFIRMED"
*                    message: "The transaction has been confirmed in the blockchain"
*                UNConfirmed:
*                  value:
*                    txid: "1234567890"
*                    code: "REQUEST_SUCCESS"
*                    status: "UNCONFIRMED"
*                    message: "The transaction is not confirmed in the blockchain"
*        "500":
*          description: 返回查询请求失败信息
*          content:
*            application/json:
*              schema:
*                $ref: '#/components/schemas/TxGetRes'
*              example:
*                txid: "43092d22d62eac0e76395aa40dcecfb27e80c61b170e4f06fc05aea27aa23aff"
*                code: "REQUEST_FAILURE"
*                message: "Failed to query the transaction status, with the error: timeout"
*/
router.get("/:id", getTx);

/** 
* @swagger 
* components:
*   schemas:
*     ArgsForDeploy:
*       description: 部署合约所需参数
*       type: object
*       properties:
*         timeout:
*           type: integer
*           description: 非负整数，部署超时(ms)（可省略，默认值5000）
*         codePackage:
*           type: string
*           description: 合约代码（不可省略）
*         legalProse: 
*           type: string
*           description: 合约法律文本（可省略，默认为空字符串）
*         codeLanguageType:
*           type: string
*           description: 合约代码类型（不可省略）
*           enum: ["CODE_JAVASCRIPT", "CODE_SCALA", "CODE_VCL_DLL", "CODE_VCL_EXE", "CODE_VCL_WASM", "CODE_WASM"]
*         runType:
*           type: string
*           description: 合约执行方式(串行/并行/系统自行决定串并行)（可省略，默认为"RUN_SERIAL"，即串行执行）
*           enum: ["RUN_SERIAL", "RUN_PARALLEL", "RUN_OPTIONAL"]
*         stateType:
*           type: string
*           description: 状态存在性证明范围（可省略，默认为"STATE_BLOCK"）
*           enum: ["STATE_BLOCK", "STATE_GLOBAL"]
*         initParameters:
*           type: string
*           description: 合约初始化方法参数（是否可省略，视具体合约初始化方法要求而定）
*       required: [codePackage, codeLanguageType]
*     ArgsForInvoke:
*       description: 调用合约所需参数
*       type: string
*     ArgsForSetState:
*       description: 更改合约有效性状态所需参数
*       type: boolean
*     TxPostRes:
*       type: object
*       properties:
*         txid:
*           type: string
*           description: 交易唯一标识
*         code:
*           type: string
*           description: 结果状态
*           enum: [REQUEST_FAILURE, REQUEST_SUCCESS] 
*         message:
*           type: string
*           description: 结果描述信息
*       required: [code, message]
*     TxGetRes:
*       type: object
*       properties:
*         txid:
*           type: string 
*           description: 交易的唯一标识
*         blockHash:
*           type: string
*           description: 被确认交易所在区块哈希值(base64编码，只有当交易状态为CONFIRMED时有值，否则undefined)
*         code:
*           type: string
*           description: 查询请求结果状态
*           enum: ["REQUEST_SUCCESS", "REQUEST_FAILURE"]
*         status:
*           type: string
*           description: 交易的状态
*           enum: ["CONFIRMED", "UNCONFIRMED"]
*         message:
*           type: string
*           description: 查询结果描述信息
*       required: [txid, code, message]
* tags:
*   name: Transactions
*   description: 请求代理构造及提交签名交易或获取交易状态
*/

/**   
* @swagger
* path:
*  /transactions:
*    post:
*       summary: 请求代理服务创建及提交签名交易
*       tags: [Transactions]
*       requestBody:
*         required: true
*         description: 签名交易构建及提交请求参数
*         content:
*           application/json:
*             schema:
*               type: object
*               properties:
*                 txid:
*                   type: string
*                   description: 指定欲生成交易的唯一标识（可省略，当省略或为空字符串时，将会自动计算交易的sha256哈希值作为交易唯一标识）
*                 oid:
*                   type: string
*                   description: 交易实例Id，用于重放举证（可省略，默认为全局实例Id）
*                 gasLimit:
*                   type: integer 
*                   minimum: 0
*                   description: 交易执行资源消耗上限（可省略，默认为无上限）
*                 txType:
*                   type: string
*                   description: 欲生成交易的类型，可为部署合约、调用合约或更改合约有效性状态（可省略）
*                   enum: [CHAINCODE_DEPLOY, CHAINCODE_INVOKE, CHAINCODE_SET_STATE]
*                 contractName:
*                   type: string
*                   description: 欲调用的合约名称（可省略，若本服务配置文件中配置有默认合约名，默认将会使用该合约名）
*                 contractVersion:
*                   type: integer
*                   minimum: 1
*                   description: 欲调用的合约版本（可省略，若本服务配置文件中配置有默认合约版本，默认将会使用该合约版本）
*                 contractFunction:
*                   type: string
*                   description: 欲调用的合约方法（可省略，若本服务配置文件中配置有默认合约方法，默认将会使用该合约方法）
*                 args:
*                   description: 部署合约、调用合约或变更合约状态所需参数（不可省略）
*                   oneOf:
*                     - $ref: '#/components/schemas/ArgsForDeploy'
*                     - $ref: '#/components/schemas/ArgsForInvoke'
*                     - $ref: '#/components/schemas/ArgsForSetState'
*               required:
*                 - args
*             examples:
*               Invoke:
*                 description: 调用合约
*                 value:
*                   txid: "1234567890"
*                   oid: ""
*                   gasLimit: 0 
*                   txType: "CHAINCODE_INVOKE"
*                   contractName: "ContractAssetsTPL"
*                   contractVersion: 1
*                   contractFunction: "putProof" 
*                   args: "{ \"key1\": \"value1\" }"
*               Deploy:
*                 description: 部署合约
*                 value:
*                   txType: "CHAINCODE_DEPLOY"
*                   contractName: "ContractAssetsTPL"
*                   contractVersion: 1
*                   contractFunction: "putProof" 
*                   args: {timeout: 5000, codePackage: "部署时本字段需要是真实的源码", legalProse: "合约相关法律问题说明", codeLanguageType: "CODE_SCALA", runType: "RUN_SERIAL", stateType: "STATE_BLOCK", initParameters: ""} 
*               SetState:
*                 description: 更改合约有效性状态
*                 value:
*                   txType: "CHAINCODE_SET_STATE"
*                   contractName: "ContractAssetsTPL"
*                   contractVersion: 1
*                   args: false 
*       responses:
*         "200":
*           description: 返回请求成功信息
*           content:
*             application/json:
*               schema:
*                 $ref: '#/components/schemas/TxPostRes'   
*               example:
*                 txid: "43092d22d62eac0e76395aa40dcecfb27e80c61b170e4f06fc05aea27aa23aff" 
*                 code: "REQUEST_SUCCESS"
*                 message: "Post the transaction successfully"
*         "400": 
*           description: 返回请求失败信息
*           content:
*             application/json:
*               schema:
*                 $ref: '#/components/schemas/TxPostRes'
*               examples:
*                 InValidTxType:
*                   value:
*                     code: "REQUEST_FAILURE"
*                     message: "Bad request, with error(s): [{\"value\":\"NO_TYPE\",\"msg\":\"The txType must be a string whose value is one of CHAINCODE_DEPLOY,CHAINCODE_INVOKE,CHAINCODE_SET_STATE, but got NO_TYPE\",\"param\":\"txType\",\"location\":\"body\"}]"
*                 InValidContractName:
*                   value:  
*                     code: "REQUEST_FAILURE"
*                     message: "Bad request, with error(s): [{\"value\":100,\"msg\":\"The contractName must be a string, but got 100\",\"param\":\"contractName\",\"location\":\"body\"}]"
*                 InValidContractVersion:
*                   value:  
*                     code: "REQUEST_FAILURE"
*                     message: "Bad request, with error(s): [{\"value\":-1,\"msg\":\"The contractVersion must be a positive number, but got -1\",\"param\":\"contractVersion\",\"location\":\"body\"}]"
*         "500": 
*           description: 返回请求失败信息
*           content:
*             application/json:
*               schema:
*                 $ref: '#/components/schemas/TxPostRes' 
*               examples:
*                 Timeout:
*                   value:
*                     txid: "43092d22d62eac0e76395aa40dcecfb27e80c61b170e4f06fc05aea27aa23aff" 
*                     code: "REQUEST_FAILURE"
*                     message: "Failed to post the transaction, with the error: post timeout" 
*                 RepChainError:
*                   value:
*                     txid: "43092d22d62eac0e76395aa40dcecfb27e80c61b170e4f06fc05aea27aa23aff" 
*                     code: "REQUEST_FAILURE"
*                     message: "Got the error message from RepChain: key1对应的值已存在" 
*
*/
router.post("/", generateTxValidators, generateTxSanitizers, generateTx);

export default router;
